package com.creditease.mobile;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import com.creditease.mobile.rss.RssConst;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-5-18
 * Time: 下午11:58
 * To change this template use File | Settings | File Templates.
 */
public class BaseActivity extends Activity {
    protected ProgressDialog mpDialog;

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setTitle(R.string.exit)
                .setMessage(R.string.exitMsg)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton(R.string.btnOK, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // System.exit(0);
                        // 完全退出
                        String packagename = getPackageName();
                        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                        finish();
                        /*
                                                       * if(getSystemVersion()<8){
                                                       *
                                                       * manager.restartPackage(
                                                       * getPackageName()); }else{
                                                       *
                                                       * manager.killBackgroundProcesses
                                                       * (packagename); }
                                                       */
                        manager.killBackgroundProcesses(packagename);
                    }
                })
                .setNegativeButton(R.string.btnCancel, null)
                .show();
    }

    public void reflushCallBack(boolean b, String... params) {

    }

    public ProgressDialog showDailog(String msg) {
        mpDialog = new ProgressDialog(this);
        mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
        mpDialog.setTitle(R.string.loadingTitle);// 设置标题
        // mpDialog.setIcon(R.drawable.icon);// 设置图标
        mpDialog.setMessage(msg);
        mpDialog.setIndeterminate(false);// 设置进度条是否为不明确
        mpDialog.setCancelable(true);// 设置进度条是否可以按退回键取消
        mpDialog.show();
        return mpDialog;
    }

    public void updatePreferences(String name, String value) {
        SharedPreferences sharedPreferences = getSharedPreferences(
                RssConst.APP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();// 获取编辑器
        editor.putString(name, value);
        editor.commit();// 提交修改
    }

    public String getPreferences(String name, String defaultValue) {
        SharedPreferences sharedPreferences = getSharedPreferences(
                RssConst.APP_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(name, defaultValue);
    }
}
